package com.boesch.electroOffice.presentation.material;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ComponentSystemEvent;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.boesch.electroOffice.business.businessObject.ArticleBusinessObject;
import com.boesch.electroOffice.business.businessObject.IdentifierBusinessObject;
import com.boesch.electroOffice.business.common.BusinessException;
import com.boesch.electroOffice.dataaccess.common.IllegalDataAccessException;
import com.boesch.electroOffice.model.electroOffice.Article;
import com.boesch.electroOffice.model.electroOffice.EldasArticle;
import com.boesch.electroOffice.model.electroOffice.Identifier;
import com.boesch.electroOffice.model.enums.SessionParameter;
import com.boesch.electroOffice.model.messerli.SteigerArticle;
import com.boesch.electroOffice.utils.PortletUtils;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

/**
 * This class controls all the data is needed for materialSteiger and materialEldas.
 * 
 * For more details about the package layering have a look at the documentation
 * of the project electroOffice or the project object model pom.xml
 * 
 * @author Ralph Koster
 *
 */
@Controller
@ManagedBean
@SessionScoped
public class MaterialController implements Serializable {

	private static final long serialVersionUID = 5742771376441992741L;
	private static final Log log = LogFactoryUtil.getLog(MaterialController.class);

	@Autowired
	private ArticleBusinessObject articleBusinessObject;
	@Autowired
	private IdentifierBusinessObject identifierBusinessObject;

	private List<EldasArticle> eldasArticles = new ArrayList<EldasArticle>();
	private List<SteigerArticle> steigerArticles = new ArrayList<SteigerArticle>();

	private List<EldasArticle> eldasAppointmentArticles = new ArrayList<EldasArticle>();
	private List<SteigerArticle> steigerAppointmentArticles = new ArrayList<SteigerArticle>();

	private List<EldasArticle> filteredEldasArticles;
	private List<SteigerArticle> filteredSteigerArticles;
	private List<EldasArticle> filteredEldasAppointmentArticles;
	private List<SteigerArticle> filteredSteigerAppointmentArticles;

	private int appointmentId = -1;

	@SuppressWarnings("unchecked")
	public void loadData(ComponentSystemEvent event) {
		String appId = PortletUtils.getSessionAttribute(SessionParameter.APPOINTMENT_ID.getValue());
		appointmentId = StringUtils.isNumeric(appId) ? Integer.parseInt(appId) : -1;
		try {
			Identifier steigerIdentifier = identifierBusinessObject.getIdentifierByEntityAndIdentifier("ARTICLE-TYPE", "STEIGER");
			Identifier eldasIdentifier = identifierBusinessObject.getIdentifierByEntityAndIdentifier("ARTICLE-TYPE", "ELDAS");

			this.eldasArticles = articleBusinessObject.readAllEldasArticles();
			this.steigerArticles = articleBusinessObject.readAllSteigerArticles();
			this.eldasAppointmentArticles = (List<EldasArticle>) articleBusinessObject.readArticlesByAppointmentIdAndIdentifierId(appointmentId, eldasIdentifier.getId());
			this.steigerAppointmentArticles = (List<SteigerArticle>) articleBusinessObject.readArticlesByAppointmentIdAndIdentifierId(appointmentId, steigerIdentifier.getId());
		} catch (BusinessException e) {
			log.error("Failed to load articles from dataaccess!", e);
		} catch (IllegalDataAccessException e) {
			log.error("Failed to load articles from dataaccess!", e);
		}
	}

	public void saveArticle(Article article) {
		try {
			if (article instanceof EldasArticle) {
				EldasArticle eldasArticle = (EldasArticle) article;
				articleBusinessObject.saveEldasArticle(eldasArticle);
			} else if (article instanceof SteigerArticle) {
				SteigerArticle steigerArticle = (SteigerArticle) article;
				articleBusinessObject.saveSteigerArticle(steigerArticle);
			}
		} catch (BusinessException e) {
			log.error("Failed to save article!", e);
		} catch (IllegalDataAccessException e) {
			log.error("Failed to save article!", e);
		}
	}

	public List<EldasArticle> getFilteredEldasArticles() {
		return filteredEldasArticles;
	}

	public void setFilteredEldasArticles(List<EldasArticle> filteredEldasArticles) {
		this.filteredEldasArticles = filteredEldasArticles;
	}

	public List<SteigerArticle> getFilteredSteigerArticles() {
		return filteredSteigerArticles;
	}

	public void setFilteredSteigerArticles(List<SteigerArticle> filteredSteigerArticles) {
		this.filteredSteigerArticles = filteredSteigerArticles;
	}

	public List<EldasArticle> getFilteredEldasAppointmentArticles() {
		return filteredEldasAppointmentArticles;
	}

	public void setFilteredEldasAppointmentArticles(List<EldasArticle> filteredEldasAppointmentArticles) {
		this.filteredEldasAppointmentArticles = filteredEldasAppointmentArticles;
	}

	public List<SteigerArticle> getFilteredSteigerAppointmentArticles() {
		return filteredSteigerAppointmentArticles;
	}

	public void setFilteredSteigerAppointmentArticles(List<SteigerArticle> filteredSteigerAppointmentArticles) {
		this.filteredSteigerAppointmentArticles = filteredSteigerAppointmentArticles;
	}

	public ArticleBusinessObject getArticleBusinessObject() {
		return articleBusinessObject;
	}

	public List<EldasArticle> getEldasArticles() {
		return eldasArticles;
	}

	public List<SteigerArticle> getSteigerArticles() {
		return steigerArticles;
	}

	public List<EldasArticle> getEldasAppointmentArticles() {
		return eldasAppointmentArticles;
	}

	public List<SteigerArticle> getSteigerAppointmentArticles() {
		return steigerAppointmentArticles;
	}
}
